/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package swingDemoEvent;

/**
 *
 * @author Administrator
 */
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;

public class JListDemo extends JFrame {
  private String[] cities = {
    "北京", "上海", "南京", "深圳",
    "济南", "沈阳", "苏州", "杭州","常州"
  };
    DefaultListModel model=new DefaultListModel();
   
 // private JList list = new JList(cities);
  private JList list = new JList();
  
  private JTextArea textArea =new JTextArea(5, 20);

  private ListSelectionListener listener =
    new ListSelectionListener() {
      public void valueChanged(ListSelectionEvent e) {
        //textArea.setText("");
        String s=(String)list.getSelectedValue();
          textArea.append(s+ "\n");
      }
    };

  public JListDemo(String title) {
    super(title);
    for(String s:cities){
        model.addElement(s);
    }
    
    list.setModel(model);
    //textArea.setEditable(false);
    list.setVisibleRowCount(5);  //在界面上显示5个选项
    
    setLayout(new FlowLayout());

    add(textArea);
    add(new JScrollPane(list)); //带滚动条
    list.addListSelectionListener(listener);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    pack();
    setVisible(true);
  }

  public static void main(String[] args) {
     new JListDemo("Hello");
  }
}
